<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
        padding: 0;
        margin: 0;
        list-style: none;
        box-sizing: border-box;
    }
    .box {
        width: 300px;
        height: 200px;
        border: 1px solid #000;
        margin: auto;
    }
    .nav{
        display: flex;
        justify-content: space-between;
    }
    .nav>li{
        flex-grow: 1;
        text-align: center;
        cursor: pointer;
    }
    .active {
        background-color: #87ceeb;
    }
    .content{
        position: relative;
    }
    .content>li{
        position: absolute;
        top: 0;
        left: 0;
        width: 100%;
        height: 178px;
        background-color: #87ceeb;
    }
    .conActive {
        z-index: 999;
    }
    </style>
</head>

<body>
    <div class="box">
        <ul class="nav">
            <li class="active">标题1</li>
            <li>标题2</li>
            <li>标题3</li>
            <li>标题4</li>
        </ul>
        <ul class="content">
            <li class="conActive">内容1</li>
            <li>内容2</li>
            <li>内容3</li>
            <li>内容4</li>
        </ul>
    </div>
    <script>
        let navli=document.querySelectorAll('.nav>li');
        let conli=document.querySelectorAll('.content>li');
        console.log(navli);
        // for(let i=0;i<navli.length;i++){
        //     navli[i].onclick=function(){
        //         for(let j=0;j<navli.length;j++){
        //             navli[j].classList.remove('active');
        //             navli[i].classList.add('active');
        //             conli[j].classList.remove('conActive');
        //             conli[i].classList.add('conActive');
        //         }   

        //     }
        // }
        for (let i = 0; i < navli.length; i++) {
            navli[i].onclick = function () {
                navli[index].classList.remove('active');
                navli[i].classList.add('active');
                conli[index].classList.remove('conActive');
                conli[i].classList.add('conActive');
                index=i
            }
        }
    </script>
</body>

</html>